                     //
//  RepairAndHomeViewController.m
//  cityStar
//
//  Created by joyban on 14-12-26.
//  Copyright (c) 2014年 tiny. All rights reserved.
//
#import "RepairAndHomeViewController.h"
#import "StayViewController.h"
#import "SGFocusImageFrame.h"
#import "SGFocusImageItem.h"
#import "UIColor+FlatUI.h"
#import "RIButtonItem.h"
#import "UIAlertView+Blocks.h"
#import "RJPhoto.h"
#import "RJPhotoViewController.h"

//商品列表头文件
#import "webView.h"
#import "PersonnelListViewController.h"
#import "CommodityListViewController.h"
//cell头文件
#import "CommunityDetailCell.h"
#import "CommunityDetailTwoCell.h"
#import "shopDetailCell.h"
#import "CollectionCell.h"
#import "StarRatingCell.h"
#import "MerchantDetail.h"
#import "UIButton+WebCache.h"
#import "INTULocationManager.h"

#import "LoginCityStarViewViewController.h"
#import "BaseNavController.h"
#import "BindingViewController.h"
#import "CitylistsModel.h"
#import "CityCommunityListViewController.h"
#import "HTDefine.h"

NSString *const HomeTableViewCellID = @"HomeCellID";
NSString *const HomeTwoTableViewCellID = @"HomeTwoCellID";
NSString *const HomeShopDetailCellID = @"HomeshopDetailCell";
NSString *const CollectionHomeTableViewCellID = @"collectionHomeCellID";
NSString *const StarRatingHomeTableViewCellID = @"starRatingHomeCellID";

@interface RepairAndHomeViewController ()<SGFocusImageFrameDelegate,CSbtnDelegate,RJPhotoViewControllerDataSource,RJPhotoViewControllerDelegate,CScommunityDelegate,CLLocationManagerDelegate>

@property (strong, nonatomic) IBOutlet UIView *heardTabelView;
@property (weak, nonatomic) IBOutlet UITableView *RepairAndHomeTableView;
@property (weak,nonatomic) NSString *status;
@property (weak,nonatomic) NSString *goodstatus;
@property (weak, nonatomic) IBOutlet UILabel *peopleNub;
@property (weak, nonatomic) IBOutlet UIButton *zidingyiBtn;
@property (strong, nonatomic)CommunityDetailCell *HomedetailCell;
@property (strong, nonatomic) NSString *merchantID;
@property (strong, nonatomic) MerchantDetail *merchantDetail;
@property (weak, nonatomic) IBOutlet UIButton *btnPhoto;
@property (strong,nonatomic) NSString *cityDistance;
@property (retain, nonatomic) CLLocationManager *locManager;
@property (nonatomic, assign) CLLocationCoordinate2D loc;
@property (nonatomic, strong) NSString *popularity;
@property (nonatomic, strong) NSArray *collectArray;
@property (weak, nonatomic) IBOutlet UIButton *looktaocan_btn;

@end

@implementation RepairAndHomeViewController

-(id)initWithMerchantID:(NSString *)merchantID
{
    self = [super init];
    if (self) {
        if(merchantID == nil)
        {
            merchantID = @"";
        }
        self.merchantID = merchantID;
    }
    return self;
}

-(void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"商户详情";
    self.zidingyiBtn.layer.cornerRadius = 4;
    self.zidingyiBtn.clipsToBounds = YES;
    self.goodstatus = @"YES";
    [self addBackButton];
    self.status = @"YES";
    //初始化位置管理器
    self.locManager = [[CLLocationManager alloc]init];
    //设置代理
    self.locManager.delegate = self;
    //设置位置经度
    self.locManager.desiredAccuracy = kCLLocationAccuracyBest;
    //设置每隔100米更新位置
    self.locManager.distanceFilter = 100;
    //开始定位服务
    [self.locManager startUpdatingLocation];
    
    [self setUpTableViews];
    [self webRequest_GetPopularity];
    [self setHeardTableView];
    [self webRequest_getDetailMessageWithDetailID:self.merchantID];
    [self webRequest_getBelowCollectMessage];
}

#pragma mark - locationDelegate
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

//协议中的方法，作用是每当位置发生更新时会调用的委托方法
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    //结构体，存储位置坐标
    self.loc = [newLocation coordinate];
    [self webRequest_getDetailMessageWithDetailID:self.merchantID];

//    float longitude = loc.longitude;
//    float latitude = loc.latitude;
    
//    CLLocation *commodityLocation = [[CLLocation alloc]initWithLatitude:[self.merchantDetail.latitude doubleValue]/10000000 longitude:[self.merchantDetail.longtitude doubleValue]/10000000];
//    CLLocation *currentLocation = [[CLLocation alloc]initWithLatitude:loc.latitude longitude:loc.longitude];;
//    CLLocationDistance kilometers=[commodityLocation distanceFromLocation:currentLocation]/1000;
//    DLog(@"%f",kilometers);
//    self.cityDistance = [NSString stringWithFormat:@"%.0f",kilometers*10000];
    
}

//当位置获取或更新失败会调用的方法
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSString *errorMsg = nil;
    if ([error code] == kCLErrorDenied) {
        errorMsg = @"访问被拒绝";
    }
    if ([error code] == kCLErrorLocationUnknown) {
        errorMsg = @"获取位置信息失败";
    }
    
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Location"
                                                      message:errorMsg delegate:self cancelButtonTitle:@"Ok"otherButtonTitles:nil, nil];
    [alertView show];
}

-(void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)addRightButtonWithImage:(UIImage *)image actionBlock:(void (^)(void))block
{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setFrame:CGRectMake(0, 0, 30, 30)];
    [rightButton setImage:image forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightBar_Touched:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.rightBarActionBlock = block;
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

-(void)webRequest_GetPopularity
{
    NSDictionary *parameters = @{@"id":self.merchantID};
    [HttpRequestManager postMethod:KinterFace_GoodsView parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
//            self.popularity = response[@"value"][0];
//            self.peopleNub.text = [NSString stringWithFormat:@"%@+人气",self.popularity];
        }
    }];
}

-(void)selectBtn
{
    if([self.status isEqualToString:@"YES"])
    {
        NSString *uuid = [[NSUserDefaults standardUserDefaults]objectForKey:@"UUID_Token"];
        if(uuid == nil)
        {
            uuid = [[NSUUID UUID] UUIDString];
            [[NSUserDefaults standardUserDefaults]setObject:uuid forKey:@"UUID_Token"];
        }
        NSDictionary *parameters = @{@"uuid":uuid,
                                     @"id":self.merchantDetail.objectId,
                                     @"type":@"1"};
        
        [HttpRequestManager postMethod:KInterface_Collection parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            DLog(@"%@",response);
            if (success) {
                [SVProgressHUD showSuccessWithStatus:@"收藏成功"];
                __weak typeof(self)weakSelf = self;
                [self addRightButtonWithImage:[UIImage imageNamed:@"collection_y"] actionBlock:^{
                    weakSelf.status = @"NO";
                    [weakSelf selectBtn];
                }];
            }else{
                if([errMsg isEqualToString:@"请先登入"])
                {
                    [self presentLoginViewController];
                }else{
                    [SVProgressHUD showErrorWithStatus:errMsg];
                }
            }
        }];

    }else{
        NSString *uuid = [[NSUserDefaults standardUserDefaults]objectForKey:@"UUID_Token"];
        NSString *postUrl = [NSString stringWithFormat:@"%@%@/%@",KInterface_RemoveCollection,uuid,@"1"];
        [HttpRequestManager deleteMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                NSLog(@"%@",response);
                [SVProgressHUD showSuccessWithStatus:@"取消收藏"];
                [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"UUID_Token"];

                __weak typeof(self)weakSelf = self;
                [self addRightButtonWithImage:[UIImage imageNamed:@"collection_n"] actionBlock:^{
                    weakSelf.status = @"YES";
                    [weakSelf selectBtn];
                }];
            }else{
                if([errMsg isEqualToString:@"请先登入"])
                {
                    [self presentLoginViewController];
                }else{
                    [SVProgressHUD showErrorWithStatus:errMsg];
                }
            }
        }];
    }
}

-(void)presentLoginViewController
{
    LoginCityStarViewViewController *loginCityStarVC = [[LoginCityStarViewViewController alloc]init];
    BaseNavController *baseloginNav = [[BaseNavController alloc] initWithRootViewController:loginCityStarVC];
    [self presentViewController:baseloginNav animated:YES completion:^{
    }];
}


//返回方法重写
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - WebRequest
-(void)webRequest_getDetailMessageWithDetailID:(NSString *)detailID
{
    NSString *postUrl = [NSString stringWithFormat:@"%@%@",KInterface_GetSupplier,detailID];
    [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            DLog(@"%@",response);
            self.merchantDetail = [MerchantDetail objectFromDictionary:response[@"value"]];
            NSString *merchantUrl = [NSString stringWithFormat:@"http://api.icszx.com/image/c/id/%@/w/640/h/300/m/1",self.merchantDetail.cover];
            
            NSString *meal = [NSString stringWithFormat:@"%@",self.merchantDetail.meal];
            NSString *product = [NSString stringWithFormat:@"%@",self.merchantDetail.product];
            if(![meal isEqualToString:@""])
            {
                [self.looktaocan_btn setTitle:meal forState:UIControlStateNormal];
            }
            if(![product isEqualToString:@""])
            {
                [self.zidingyiBtn setTitle:product forState:UIControlStateNormal];
            }
            
            [self.btnPhoto sd_setBackgroundImageWithURL:[NSURL URLWithString:merchantUrl] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"品牌详情_主图"]];
            self.peopleNub.text = [NSString stringWithFormat:@"%@+人气",self.merchantDetail.views];
//            CLLocation *testLocayion = [INTULocationManager sharedInstance].currentLocation;
            CLLocation *commodityLocation = [[CLLocation alloc]initWithLatitude:[self.merchantDetail.latitude doubleValue]/10000000 longitude:[self.merchantDetail.longtitude doubleValue]/10000000];
            CLLocation *currentLocation = [[CLLocation alloc]initWithLatitude:self.loc.latitude longitude:self.loc.longitude];
            CLLocationDistance kilometers=[commodityLocation distanceFromLocation:currentLocation]/1000;
            DLog(@"%f",kilometers);
            self.cityDistance = [NSString stringWithFormat:@"%.0f",kilometers*10000];
            
            NSString *uuid = @"";
            __weak typeof(self)weakSelf = self;
            if(self.merchantDetail.collection != nil&& ![self.merchantDetail.collection isEqualToString:@""])
            {
                [self addRightButtonWithImage:[UIImage imageNamed:@"collection_y"] actionBlock:^{
                    weakSelf.status = @"NO";
                    [weakSelf selectBtn];
                }];
                uuid = self.merchantDetail.collection;
                
            }else{
                [self addRightButtonWithImage:[UIImage imageNamed:@"collection_n"] actionBlock:^{
                    weakSelf.status = @"YES";
                    [weakSelf selectBtn];
                }];
                uuid = [[NSUUID UUID] UUIDString];
            }
            if([[NSUserDefaults standardUserDefaults]objectForKey:@"UUID_Token"]!=nil)
            {
                [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"UUID_Token"];
            }
            
            [[NSUserDefaults standardUserDefaults]setObject:uuid forKey:@"UUID_Token"];
            
            [self.RepairAndHomeTableView reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:errMsg];
        }
        
    }];
}

-(void)webRequest_getBelowCollectMessage
{
    NSString *postUrl = [NSString stringWithFormat:@"%@%@",KinterFace_RecommentSupplier,[KStandUserDefaults objectForKey:KUserDefaultKey_PersonalCityID]?KinterFace_RecommentSupplier,[KStandUserDefaults objectForKey:KUserDefaultKey_PersonalCityID]:@"122"];
    [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            DLog(@"%@",response);
            self.collectArray = [[NSArray alloc]initWithArray:response[@"value"]];
        }else
        {
            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    }];
}

#pragma mark - collectionCell btndelegate

-(void)sendergoodClick:(UIButton *)btn
{
    if([self.goodstatus isEqualToString:@"YES"])
    {
        NSDictionary *dict = @{@"id":self.merchantID,
                               @"type":@"supplier"
                               };
        [HttpRequestManager postMethod:KinterFace_PraiseOne parameters:dict completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                if (success) {
                    DLog(@"%@",response);
                    [SVProgressHUD showSuccessWithStatus:@"点赞成功"];
                    [self.HomedetailCell.goodBtn setImage:[UIImage imageNamed:@"good_y"] forState:UIControlStateNormal];
                    self.HomedetailCell.goodLabel.text = [NSString stringWithFormat:@"%i赞",[self.merchantDetail.parise intValue]+1];
                }else{
                    [SVProgressHUD showErrorWithStatus:errMsg];
                }
            }
        }];
//        self.HomedetailCell.numbelLabel.textColor = [UIColor colorFromHexCode:@"FF4C4C"];
//        NSString *string = [self.HomedetailCell.numbelLabel.text substringToIndex:self.HomedetailCell.numbelLabel.text.length -1];
//        NSString *thestr = [NSString stringWithFormat:@"%d",[string intValue]+1];
//        self.HomedetailCell.numbelLabel.text = [thestr stringByAppendingString:@"赞"] ;
//        self.goodstatus = @"NO";
    }else{
//        [SVProgressHUD showSuccessWithStatus:@"取消点赞"];
//        [self.HomedetailCell.goodBtn setImage:[UIImage imageNamed:@"good_n"] forState:UIControlStateNormal];
//        self.HomedetailCell.numbelLabel.textColor = [UIColor colorFromHexCode:@"949494"];
//        NSString *string = [self.HomedetailCell.numbelLabel.text substringToIndex:self.HomedetailCell.numbelLabel.text.length -1];
//        NSString *thestr = [NSString stringWithFormat:@"%d",[string intValue]-1];
//        self.HomedetailCell.numbelLabel.text = [thestr stringByAppendingString:@"赞"] ;
//        self.goodstatus = @"YES";
    }
}

-(IBAction)productbtn_Action:(UIButton *)sender {
    CommodityListViewController *gooodsListViewController = [[CommodityListViewController alloc] init];
    gooodsListViewController.shopid =  self.merchantID;
    [self.navigationController pushViewController:gooodsListViewController animated:true];
}

-(IBAction)mealbtn_Action:(UIButton *)sender {
//    CityCommunityListViewController *cityCommunityListVC = [[CityCommunityListViewController alloc]initWithMerchartID:self.merchantDetail.meal];
    CommodityListViewController *gooodsListViewController = [[CommodityListViewController alloc] init];
    gooodsListViewController.shopid =  self.merchantID;
    [self.navigationController pushViewController:gooodsListViewController animated:true];
}

-(void)senderBtnClick:(UIButton *)btn
{
    
    RepairAndHomeViewController *personnelListViewController = [[RepairAndHomeViewController alloc] initWithMerchantID:[NSString stringWithFormat:@"%li",(long)btn.tag]];
    [self.navigationController pushViewController:personnelListViewController animated:true];
}

-(IBAction)btnPhoto_Action:(id)sender
{
//    RJPhotoViewController *photoVC = [[RJPhotoViewController alloc] initWithDataSource:self delegate:self];
//    photoVC.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:photoVC animated:YES];
}

#pragma mark - RJPhotoView DataSource
-(NSInteger)photoViewNumberOfPhoto
{
    //    return [self.currentMoment.imgList count];
    return 5;
}

-(RJPhoto *)photoView:(RJPhotoViewController *)photoView photoForRow:(NSUInteger)row
{
    RJPhoto *photo = [[RJPhoto alloc] init];
    //    MomentImg *momentImg = self.currentMoment.imgList[row];
    photo.URL = [NSURL URLWithString:@"http://classimage.b0.upaiyun.com/201412/jre5hnts.png"];
    
    photo.title = @"hello world";
    photo.caption = @"zxznzxncjkjncskjdcnskjdnckjdsnkjccszx";
    return photo;
}

#pragma mark - RJPhotoView Delegate
-(NSInteger)numberOfCommentCount
{
    //    return [self.currentMoment.commCount integerValue];
    return 5;
}

-(void)photoViewDidSelectCommentButton
{
    //    [self _goToCommentListVCWithMoment:self.currentMoment];
}

-(void)_goToCommentListVCWithMoment:(Moment *)moment
{
    //    CommentListVC *commentListVC = [[CommentListVC alloc] initWithMoment:moment];
    //    [self.navigationController pushViewController:commentListVC animated:YES];
}

#pragma mark - 轮转

-(void)setHeardTableView
{
    self.RepairAndHomeTableView.tableHeaderView = self.heardTabelView;
    self.peopleNub.layer.cornerRadius = 10;
    self.peopleNub.clipsToBounds = YES;
}

-(void)foucusImageFrame:(SGFocusImageFrame *)imageFrame didSelectItem:(SGFocusImageItem *)item ImagePage:(int)pageIndex
{
    
}

-(void)setUpTableViews
{
    [self.RepairAndHomeTableView registerNib:[CommunityDetailCell nib] forCellReuseIdentifier:HomeTableViewCellID];
    [self.RepairAndHomeTableView registerNib:[CommunityDetailTwoCell nib] forCellReuseIdentifier:HomeTwoTableViewCellID];
    [self.RepairAndHomeTableView registerNib:[shopDetailCell nib] forCellReuseIdentifier:HomeShopDetailCellID];
    [self.RepairAndHomeTableView registerNib:[CollectionCell nib] forCellReuseIdentifier:CollectionHomeTableViewCellID];
    [self.RepairAndHomeTableView registerNib:[StarRatingCell nib] forCellReuseIdentifier:StarRatingHomeTableViewCellID];
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
//        self.merchantDetail.name = @"杭州路易威登杭州飒飒是路易威登杭杭州路易威登";
        NSString *ss = [NSString stringWithFormat:@"%@",self.merchantDetail.name];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 220, 25)];
        label.font = [UIFont systemFontOfSize:15];
        [label setLineBreakMode:NSLineBreakByWordWrapping];
        [label setNumberOfLines:0];
        NSString *_Detail = [NSString stringWithFormat:@"%@",ss];
        label.text = _Detail;
        [label sizeToFit];
        float labelheight = label.frame.size.height;
        
        NSArray *refundArray = [[NSString stringWithFormat:@"%@",self.merchantDetail.refund] componentsSeparatedByString:@","];

        if([refundArray[0]isEqualToString:@"0"])
        {
            return [CommunityDetailCell heightOfTableCell]-12-(18-labelheight);
        }else{
            return [CommunityDetailCell heightOfTableCell] - (18 -labelheight);
        }
    }else if(indexPath.section == 1)
    {
        return [CommunityDetailTwoCell heightOfTableCell];
    }else if(indexPath.section == 2)
    {
        return [StarRatingCell heightOfTableCell];
    }else if(indexPath.section == 3)
    {
        return [shopDetailCell heightOfTableCell];
    }else{
        return [CollectionCell heightOfTableCell];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0 || section == 3)
    {
        return 0;
    }else{
        return 30;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 1)
    {
        return 3;
    }else{
        return 1;
        
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 220)] ;
    [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"F6F5F1"]];
    return sectionView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel * label = [[UILabel alloc] init] ;
    label.frame = CGRectMake(15, 5, 200, 20);
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor colorFromHexCode:@"333333"];
    label.font=[UIFont systemFontOfSize:14];
    
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 220)] ;
    [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"FFF3E2"]];
    [sectionView addSubview:label];
    
    if(section==1)
    {
        label.text = @"基本信息";
    }else if(section == 2)
    {
        label.text = @"星级评分";
    }else if(section == 4)
    {
        UIImageView *imageOne = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 1)];
        [imageOne setBackgroundColor:[UIColor colorFromHexCode:@"ECECEC"]];
        
        UIImageView *imageTwo = [[UIImageView alloc]initWithFrame:CGRectMake(0, 29, tableView.bounds.size.width, 1)];
        [imageTwo setBackgroundColor:[UIColor colorFromHexCode:@"ECECEC"]];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 5, 20, 20)];
        [imageView setImage:[UIImage imageNamed:@"recom"]];
        [sectionView addSubview:imageView];
        [sectionView addSubview:imageOne];
        [sectionView addSubview:imageTwo];
        [label setFrame:CGRectMake(45, 5, 200, 20)];
        label.textColor = [UIColor colorFromHexCode:@"8E8E8E"];
        [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"EEEEEE"]];
        label.text = @"到过这里的人还看过";
    }
    return sectionView;
}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        self.HomedetailCell = [tableView dequeueReusableCellWithIdentifier:HomeTableViewCellID];
        self.HomedetailCell.selectionStyle = UITableViewCellSelectionStyleNone;
        self.HomedetailCell.delegate = self;
        [self.HomedetailCell configWithMerchant:self.merchantDetail];
        return self.HomedetailCell;
        
    }else if(indexPath.section == 1)
    {
        CommunityDetailTwoCell *HomedetailTwoCell = [tableView dequeueReusableCellWithIdentifier:HomeTwoTableViewCellID];
        if (HomedetailTwoCell==nil) {
            HomedetailTwoCell=[[CommunityDetailTwoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:HomeTwoTableViewCellID];
        }
        //根据每行修改数据
        if(indexPath.row == 0)
        {
            HomedetailTwoCell.accessoryType = UITableViewCellAccessoryNone;
            HomedetailTwoCell.selectionStyle = UITableViewCellSelectionStyleNone;
            [HomedetailTwoCell.heardImage setImage:[UIImage imageNamed:@"range"]];
//            HomedetailTwoCell.contentLabel.text = @"距离您的位置200m";
            NSString *strone = @"距离您的位置  ";
            NSString *strTwo = @"";
            if ([self.cityDistance intValue] > 3000) {
                strTwo = @">3000m";
            }else{
            strTwo = [NSString stringWithFormat:@"%@m",self.cityDistance];
            }
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",strone,strTwo]];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorFromHexCode:@"FF7C08"] range:NSMakeRange(strone.length,strTwo.length)];
            HomedetailTwoCell.contentLabel.attributedText = str ;
        }
        else if(indexPath.row == 1)
        {
            HomedetailTwoCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            HomedetailTwoCell.selectionStyle = UITableViewCellSelectionStyleGray;
            [HomedetailTwoCell.heardImage setImage:[UIImage imageNamed:@"list_tel"]];
            if (self.merchantDetail.telphone) {
                HomedetailTwoCell.contentLabel.text = [NSString stringWithFormat:@"%@",self.merchantDetail.telphone];
            }else if(self.merchantDetail.service_mobile){
                HomedetailTwoCell.contentLabel.text = [NSString stringWithFormat:@"%@",self.merchantDetail.service_mobile];
            }else{
                HomedetailTwoCell.contentLabel.text = [NSString stringWithFormat:@"%@",self.merchantDetail.mobile];
            }
        }else{
            HomedetailTwoCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            HomedetailTwoCell.selectionStyle = UITableViewCellSelectionStyleGray;
            HomedetailTwoCell.contentLabel.text = [NSString stringWithFormat:@"%@",self.merchantDetail.address ];
            [HomedetailTwoCell.lineImage removeFromSuperview];
        }
        return HomedetailTwoCell;
    }
    else if(indexPath.section == 2)
    {
        StarRatingCell *StarRatingHomeCell = [tableView dequeueReusableCellWithIdentifier:StarRatingHomeTableViewCellID];
        StarRatingHomeCell.selectionStyle = UITableViewCellSelectionStyleNone;
        [StarRatingHomeCell configWithCredit:[[NSString stringWithFormat:@"%@",self.merchantDetail.credit] integerValue]];
        return StarRatingHomeCell;
        
    }else if(indexPath.section == 3)
    {
        shopDetailCell *ShopDetailCell = [tableView dequeueReusableCellWithIdentifier:HomeShopDetailCellID];
        ShopDetailCell.selectionStyle = UITableViewCellSelectionStyleGray;
        return ShopDetailCell;
        
    }else{
        CollectionCell *collectionHomeCell = [tableView dequeueReusableCellWithIdentifier:CollectionHomeTableViewCellID];
        collectionHomeCell.delegate = self;
        collectionHomeCell.selectionStyle = UITableViewCellSelectionStyleNone;
        [collectionHomeCell configWithMessage:self.collectArray];
        if(collectionHomeCell.NameLabelOne.text.length > 13)
        {
            NSString *btntext = [NSString stringWithFormat:@"%@...",[collectionHomeCell.NameLabelOne.text substringWithRange:NSMakeRange(0, 13)]];
            ;
            collectionHomeCell.NameLabelOne.text = btntext;
        }
        if(collectionHomeCell.NameLabelTwo.text.length > 13)
        {
            NSString *btntext = [NSString stringWithFormat:@"%@...",[collectionHomeCell.NameLabelTwo.text substringWithRange:NSMakeRange(0, 13)]];
            ;
            collectionHomeCell.NameLabelTwo.text = btntext;
        }
        if(collectionHomeCell.NameLabelThree.text.length > 13)
        {
            NSString *btntext = [NSString stringWithFormat:@"%@...",[collectionHomeCell.NameLabelThree.text substringWithRange:NSMakeRange(0, 13)]];
            ;
            collectionHomeCell.NameLabelThree.text = btntext;
        }
        if(collectionHomeCell.NameLabelFour.text.length > 13)
        {
            NSString *btntext = [NSString stringWithFormat:@"%@...",[collectionHomeCell.NameLabelFour.text substringWithRange:NSMakeRange(0, 13)]];
            ;
            collectionHomeCell.NameLabelFour.text = btntext;
        }
        return collectionHomeCell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 1)
    {
        if(indexPath.row == 1)
        {
            RIButtonItem *leftButton = [RIButtonItem itemWithLabel:@"确定" action:^{
                NSString *telStr=[@"tel://" stringByAppendingString:self.merchantDetail.telphone];
                NSURL *url=[NSURL URLWithString:telStr];
                [[UIApplication sharedApplication]openURL:url];
            }];
            RIButtonItem *rightButton = [RIButtonItem itemWithLabel:@"取消"];
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"是否拨打该号码？" message:self.merchantDetail.telphone cancelButtonItem:rightButton otherButtonItems:leftButton, nil];
            [alertView show];
        }else if (indexPath.row == 2){
            CitylistsModel *cityModel = [[CitylistsModel alloc]init];
            NSDictionary *locationDic = [[NSDictionary alloc]initWithObjectsAndKeys:[NSString stringWithFormat:@"%f",[self.merchantDetail.latitude floatValue]/10000000],
                                         @"y",[NSString stringWithFormat:@"%f",[self.merchantDetail.longtitude floatValue]/10000000],@"x", nil];
            cityModel.location = [NSDictionary dictionaryWithDictionary:locationDic];
            cityModel.name = self.merchantDetail.name;
            BindingViewController *bindingVC = [[BindingViewController alloc]initWithLocationMessage:cityModel andRightBtn:NO];
            bindingVC.navigationItem.title = self.merchantDetail.name;
            
            [self.navigationController pushViewController:bindingVC animated:YES];
        }
    }
    if(indexPath.section == 3)
    {
        NSString *btntext = @"";
        if(self.merchantDetail.name.length > 6)
        {
            btntext = [NSString stringWithFormat:@"%@...",[self.merchantDetail.name substringWithRange:NSMakeRange(0, 6)]];
        }else{
            btntext = [NSString stringWithFormat:@"%@",self.merchantDetail.name];
        }
        
        if([self.merchantDetail.name isEqualToString:@""] || self.merchantDetail.name == nil)
        {
            btntext = @"商户详情";
        }
        
        webView *webViewController = [[webView alloc]init];
        webViewController.navTitle = btntext;
        webViewController.type = @"shop";
        webViewController.goodsid = self.merchantID;
        [self.navigationController pushViewController:webViewController animated:YES];
    }
    [tableView reloadData];
}
 /*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
